﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Diagnostics;
public partial class UserWork_Vindicate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string UserName = Session["UserName"].ToString();
            string sql = "select * from Grouping where username='"+UserName+"' ";
            SqlDataAdapter da = new SqlDataAdapter(sql,con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.DropDownList1.DataSource = ds;
            this.DropDownList1.DataTextField = "Type";
            DropDownList1.DataValueField = "groupingid";
            DropDownList1.DataBind();
            con.Close();
        }
    }
    public void Bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from AddressList where username='"+Session["UserName"].ToString()+"'";
        SqlDataAdapter da = new SqlDataAdapter(sql,con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
        this.Label1.Text = "共"+ds.Tables[0].Rows.Count.ToString()+"条记录";
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[7].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确认要删除吗?')");
            }
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        int Sid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string sql = "delete from AddressList where sid="+Sid+"";
        SqlCommand com = new SqlCommand(sql,con);
        com.ExecuteNonQuery();
        Response.Write("<script>alert('删除成功!!')</script>");
        Bind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.txName.Value.Trim() == "")
        {
            Response.Write("<script>alert('对不起,请输入查询条件!')</script>");
        }
        else
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            int Groupingid = Convert.ToInt32(DropDownList1.SelectedItem.Value.ToString());
            string name = this.txName.Value.Trim();
            string sql = "select * from AddressList where Groupingid=" + Groupingid + " and name like('%" + name + "%')";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('对不起,没有你查询的人！')</script>");
            }
            else
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
                this.Label1.Text = "共" + ds.Tables[0].Rows.Count + "记录";
            }
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string Sid = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
        Response.Redirect("InfoPub.aspx?Sid=" + Sid + "");
      //  Response.Write("<script>window.loaction.href='InfoPub.aspx';</script>");
      //  string Sid = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
      //  Response.Write("<script>window.loaction='InfoPub.aspx?id=" + Sid + "';</script>");
    
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string Sid = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        Response.Redirect("InfoView.aspx?sid="+Sid+"");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Process proc = new Process();
        proc.StartInfo.FileName = @"E:\OA_Test\allBoBo.exe";
        proc.StartInfo.Arguments = "";
        proc.Start();
    }
}
